package cn.huihuo.weibo.service;

import cn.huihuo.weibo.bean.comment.ChooseResult;
import cn.huihuo.weibo.module.ProdComment;
import cn.huihuo.weibo.module.UserChooseChannel;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * Created by zhengjunbo on 2015/10/28.
 */
public class ChooseChannelService {
    public static final ChooseChannelService service= new ChooseChannelService();
    @Before(Tx.class)
    public synchronized ChooseResult findChooseResult(Integer ugcId,String userId,Integer chooseChannel){
        //查看是否已经选择，如果没有选择，则插入选择
        //如果已经选择，则更新选择，并且更新数量



        UserChooseChannel userChooseChannel = UserChooseChannel.dao.getUserChooseChannel(ugcId,userId);
        if(userChooseChannel==null){
            saveChannel(ugcId, userId, chooseChannel);

        } else {
            //更新
            Integer userChooseChannelInt = userChooseChannel.getInt("choose_channel");
            if(userChooseChannelInt==1&&chooseChannel==2){
                ProdComment.prodComment.updateGoodChannel(ugcId,-1,1,0);
            } else if (userChooseChannelInt==1&&chooseChannel==3){
                ProdComment.prodComment.updateGoodChannel(ugcId,-1,0,1);
            } else if (userChooseChannelInt==2&&chooseChannel==1){
                ProdComment.prodComment.updateGoodChannel(ugcId,1,-1,0);
            } else if (userChooseChannelInt==2&&chooseChannel==3){
                ProdComment.prodComment.updateGoodChannel(ugcId,0,-1,1);
            } else if (userChooseChannelInt==3&&chooseChannel==1){
                ProdComment.prodComment.updateGoodChannel(ugcId,1,0,-1);
            } else if (userChooseChannelInt==3&&chooseChannel==2){
                ProdComment.prodComment.updateGoodChannel(ugcId,0,1,-1);
            }

//            else if(userChooseChannelInt==1&&chooseChannel==1){
//                ProdComment.prodComment.updateGoodChannel(ugcId,-1,0,0);
//                chooseChannel = 0;
//            } else if(userChooseChannelInt==2&&chooseChannel==2){
//                ProdComment.prodComment.updateGoodChannel(ugcId,0,-1,0);
//                chooseChannel = 0;
//            } else if(userChooseChannelInt==3&&chooseChannel==3){
//                ProdComment.prodComment.updateGoodChannel(ugcId,0,0,-1);
//                chooseChannel = 0;
//            }
            UserChooseChannel.dao.updateUserChooseChannel(userChooseChannel.getInt("id"),chooseChannel);
        }

        ProdComment prodComment = ProdComment.prodComment.findById(ugcId);
        if(prodComment==null){
            throw new RuntimeException("系统数据异常");
        }
        ChooseResult chooseResult = new ChooseResult();
        chooseResult.setChooseChannel(String.valueOf(chooseChannel));
        chooseResult.setGoodChannelNum(String.valueOf(prodComment.getInt("good_channel")));
        chooseResult.setNormalChannelNum(String.valueOf(prodComment.getInt("normal_channel")));
        chooseResult.setBadChannelNum(String.valueOf(prodComment.getInt("bad_channel")));
        return chooseResult;
    }

    private void saveChannel(Integer ugcId, String userId, Integer chooseChannel) {
        //插入数据并且加一
        if(chooseChannel==1){
            ProdComment.prodComment.updateGoodChannel(ugcId,1,0,0);
        } else if(chooseChannel ==2){
            ProdComment.prodComment.updateGoodChannel(ugcId,0,1,0);
        } else if( chooseChannel==3 ){
            ProdComment.prodComment.updateGoodChannel(ugcId,0,0,1);
        }
        UserChooseChannel userChooseChannel1 = new UserChooseChannel();
        userChooseChannel1.set("ugc_id",ugcId)
                .set("choose_channel",chooseChannel)
                .set("user_id",userId).save();
    }
}
